﻿using DAL;
using DAL.Dao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using System.Web.Script.Serialization;
using DataProj.Areas.BackSys.ServiceTools;
using Newtonsoft.Json;

namespace DataProj.Areas.BackSys.Controllers
{
    public class UserController : BaseController
    {
        // GET: UserInfo
        // GET: /BackSys/UserInfo/
        private static UserinfoImpl _impl;
        public UserController()
        {
            if (_impl == null) _impl = new UserinfoImpl();
            if (string.IsNullOrEmpty(MySession.GetSession("userRows")))
            {
                MySession.WriteSession("userRows", 10);
            }
            if (string.IsNullOrEmpty(MySession.GetSession("userPage")))
            {
                MySession.WriteSession("userPage", 1);
            }

        }

        public ActionResult UserManage()
        {

            //(var totalPages, var data) = _impl.GetList(Convert.ToInt16(MySession.GetSession("userRows")), Convert.ToInt16(MySession.GetSession("userPage")));
            //ViewBag.UserList = data;
            //MySession.WriteSession("userTotalPages", totalPages);
            return PartialView();
        }

        public ActionResult UserUpdate()
        {
            return PartialView();
        }


        [HttpPost]
        public ActionResult ChangePage()
        {
            IDictionary<string, string> set = new Dictionary<string, string>();
            int page = Convert.ToInt16(Request["pageNum"].Trim());
            //MySession.WriteSession("userPage", page) ;
            (var totalPages, var data) = _impl.GetList(Convert.ToInt16(MySession.GetSession("userRows")), page);
            //MySession.WriteSession("userTotalPages", totalPages);
          
            //return Json(JsonConvert.SerializeObject(data));

            set.Add("totalPages", totalPages.ToString());
            set.Add("data", JsonConvert.SerializeObject(data));
            return Json(JsonConvert.SerializeObject(set));
        }
        public ActionResult List()
        {
            int page = 1;
            if (!string.IsNullOrWhiteSpace(Request["userPage"]))
            {
                if (int.TryParse(Request["userPage"], out page))
                {
                    if (page < 1)
                    {
                        page = 1;
                    }
                };
            }
            int rows = 10;
            if (!string.IsNullOrWhiteSpace(Request["userRows"]))
            {
                if (int.TryParse(Request["userRows"], out rows))
                {
                    if (rows < 10)
                    {
                        rows = 10;
                    }
                    else if (rows > 30)
                    {
                        rows = 30;
                    }
                }
            }
            return Json(new
            {
                rows = _impl.GetList(rows,page),
                total = _impl.ExistNum
            },
                JsonRequestBehavior.AllowGet);
       
        }
        //
        // GET: /BackSys/UserInfo/Details/5
        [HttpPost]
        public ActionResult GetItemByID(string id,string action)
        {
            UserInfo user = _impl.Find(Convert.ToInt32(id));
            var tool = new JavaScriptSerializer();
            //string get = MyRequest.GetQueryString();
            //Task obj = tool.DeserializeObject(get) as Task;
            if (user == null)
            {
                return Json(null);
            }
            string seri = tool.Serialize(user);
            return Json(seri);

        }

        //
        // GET: /BackSys/UserInfo/Create

        public ActionResult Create()
        {
            return PartialView();
        }

        //
        // POST: /BackSys/UserInfo/Create

        [HttpPost]
        public ActionResult Create(UserInfo user)
        {
            if (ModelState.IsValid)
            {
                _impl.Add(user);
                return RedirectToAction("Index");
            }
           
            return PartialView(user);
        }


        public ActionResult UserRegister()
        {
            return PartialView();
        }
        public ActionResult RegAdd()
        {
            UserInfo user = new UserInfo();
            user.UserName = Request["UserName"].Trim();

            user.UserPass = Request["Password"].Trim();
            UserInfo userFind = _impl.Login(user.UserName, user.UserPass); 
            if (userFind != null)
            {
                return Content("该用户已经存在，请更换用户名！");
            }
            _impl.Add(user);

            return Content(Common.SUCCESS.ToString());
        }

        public ActionResult Profile()
        {
            return PartialView();

        }
        [HttpPost]
        public ActionResult Add()
        {
            UserInfo user = new UserInfo();
            user.UserName = Request["UserName"];
            user.UserPass = Request["UserPass"];
            user.TrueName = Request["TrueName"];
            user.Phone = Request["Phone"];
            user.Email = Request["Email"];
            user.IDCard = Request["IDCard"];
            user.ContactAddress = Request["Address"];
            user.AddTime = DateTime.Now;
            if (_impl.Add(user)) return Json(1);
            
            return Json(0);
        }
        [HttpPost]
        public ActionResult Update()
        {
            UserInfo user = new UserInfo();
            user.ID = Convert.ToInt32(Request["ID"]);
            user.UserName = Request["UserName"];
            //user.UserPass = Request["UserPass"];
            user.TrueName = Request["TrueName"];
            user.Phone = Request["Phone"];
            user.Email = Request["Email"];
            //user.IDCard = Request["IDCard"];
            user.ContactAddress = Request["Address"];
            user.UpdateTime = DateTime.Now;
            if (_impl.Update(user.ID, user)==true)
            {
                return Json(1);
            }


            return Json(0);
        }
        //
        // GET: /BackSys/UserInfo/Edit/5

        public ActionResult Edit(int id = 0)
        {
            UserInfo user = _impl.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return PartialView(user);
        }

        //
        // POST: /BackSys/UserInfo/Edit/5


        public ActionResult Login()
        {
            UserInfo user = new UserInfo();
            user.UserName = Request["UserName"].Trim();
            //if(
            user.UserPass = Request["Password"].Trim();
            UserInfo find = _impl.Login(user.UserName, user.UserPass);
            if (find == null)
            {
                return Content(Common.FAIL.ToString());
            }
            else
            {
                user.UserPass = null;
                Session[Common.UserLoginSessionKey] = user;
                return Content(Common.SUCCESS.ToString());
            }
        }
        //
        // GET: /BackSys/UserInfo/Delete/5
        [HttpPost]
        public ActionResult Delete(int id = 0)
        {
         
            if (_impl.Delete(id)==true)
            {
                return Json(1);
            }
            return Json(0);
        }
        public ActionResult Remove()
        {
            int id = 0;
            if (int.TryParse(Request["ID"], out id))
            {
                _impl.Delete(id);
                return Content("删除用户成功！");
            }
            return Content("删除失败");
        }
        //
        // POST: /BackSys/UserInfo/Delete/5



        protected override void Dispose(bool disposing)
        {
            DbEntity.Dispose();
            base.Dispose(disposing);
        }
    }
}